package com.controller;

import com.model.ResponseMsg;
import com.model.SystemMessage;
import com.model.*;

import com.util.ConstantUtil;
import dubbo.PageDataService;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
public class PageController {

    @Autowired
    private PageDataService pageDataService;

    /**
     * 首页
     *
     * @return index.html
     */
    @RequestMapping("/")
    public ModelAndView indexView(HttpSession session) {
        ModelAndView modelAndView = new ModelAndView("index");
        modelAndView.addObject("pageName", "");
        // 服务器时间
        ResponseMsg responseMsg = pageDataService.systemTimeByClient(new Users());
        modelAndView.addObject("currentTime", responseMsg.getData());
        responseMsg = pageDataService.systemNoticeByClient();
        modelAndView.addObject("referenceData", responseMsg.getData());
        responseMsg = pageDataService.bannerListByClient();
        modelAndView.addObject("bannerData", responseMsg.getData());
        responseMsg = pageDataService.moneyListByClient();
        modelAndView.addObject("moneyData", responseMsg.getData());
        Users userPage = (Users) session.getAttribute(ConstantUtil.loginSession);
        if (userPage != null) {
        }
        return modelAndView;
    }

    /**
     * 退出登录
     *
     * @param session
     * @return
     */
    @RequestMapping("/logout")
    public ModelAndView logout(HttpSession session) {
        session.invalidate();
        return new ModelAndView(new RedirectView("/"));
    }

    /**
     * 注册
     *
     * @param session
     * @param model
     * @return
     */
    @RequestMapping("/register")
    public ModelAndView registerView(HttpSession session, Map<String, Object> model) {
        ModelAndView modelAndView = new ModelAndView("register");
        ResponseMsg responseMsg = pageDataService.systemTimeByClient(new Users());
        modelAndView.addObject("currentTime", responseMsg.getData());
        return modelAndView;
    }


    /**
     * 忘记密码 - 第一步
     *
     * @param session
     * @param model
     * @return
     */
    @RequestMapping("/forgotpasswordone")
    public ModelAndView forgotpasswordoneView(HttpSession session, Map<String, Object> model) {
        ModelAndView modelAndView = new ModelAndView("forgotpasswordone");
        ResponseMsg responseMsg = pageDataService.systemTimeByClient(new Users());
        modelAndView.addObject("currentTime", responseMsg.getData());
        return modelAndView;
    }

    /**
     * 忘记密码 - 第二步
     *
     * @param session
     * @param model
     * @return
     */
    @RequestMapping("/forgotpasswordtwo")
    public ModelAndView forgotpasswordtwoView(HttpSession session, Map<String, Object> model) {
        Users forgotUserInfo = (Users) session.getAttribute(ConstantUtil.FORGOT_USER_SESSION);
        if (forgotUserInfo != null) {
            ModelAndView modelAndView = new ModelAndView("forgotpasswordtwo");
            ResponseMsg responseMsg = pageDataService.systemTimeByClient(new Users());
            modelAndView.addObject("currentTime", responseMsg.getData());
            return modelAndView;
        } else {
            return new ModelAndView(new RedirectView("/forgotpasswordone"));
        }
    }

    /**
     * 忘记密码 - 第三步
     *
     * @param session
     * @param model
     * @return
     */
    @RequestMapping("/forgotpasswordthree")
    public ModelAndView forgotpasswordthreeView(HttpSession session, Map<String, Object> model) {
        ModelAndView modelAndView = new ModelAndView("forgotpasswordthree");
        ResponseMsg responseMsg = pageDataService.systemTimeByClient(new Users());
        modelAndView.addObject("currentTime", responseMsg.getData());
        return modelAndView;
    }

    /**
     * 存款
     *
     * @return
     */
    @RequestMapping("/deposit")
    public ModelAndView depositView(HttpSession session, Map<String, Object> model) {
        Users userPage = (Users) session.getAttribute(ConstantUtil.loginSession);
        ModelAndView modelAndView = new ModelAndView("userCenter");
        ResponseMsg responseMsg = pageDataService.systemTimeByClient(userPage);
        modelAndView.addObject("currentTime", responseMsg.getData());
        SystemMessage SystemMessage = new SystemMessage();
        SystemMessage.setUserId(userPage.getUserId());
        responseMsg =  pageDataService.systemUnreadMessageByClient(SystemMessage);
        modelAndView.addObject("unReadMsgNum", responseMsg.getData());
        modelAndView.addObject("pagename", "deposit");
        responseMsg = pageDataService.depositChannelByClientUserId(userPage);
        modelAndView.addObject("depositChannelList", responseMsg.getData());
        return modelAndView;
    }

    /**
     * 提款
     *
     * @return
     */
    @RequestMapping("/withdrawal")
    public ModelAndView withdrawalView(HttpSession session, Map<String, Object> model) {
        Users userPage = (Users) session.getAttribute(ConstantUtil.loginSession);
        ModelAndView modelAndView = new ModelAndView("userCenter");
        ResponseMsg responseMsg = pageDataService.systemTimeByClient(new Users());
        modelAndView.addObject("currentTime", responseMsg.getData());
        SystemMessage SystemMessage = new SystemMessage();
        SystemMessage.setUserId(userPage.getUserId());
        responseMsg =  pageDataService.systemUnreadMessageByClient(SystemMessage);
        modelAndView.addObject("unReadMsgNum", responseMsg.getData());
        modelAndView.addObject("pagename", "withdrawal");

        UserBank userBank = new UserBank();
        userBank.setUserId(userPage.getUserId());
        responseMsg = pageDataService.userBankList(userBank);
        modelAndView.addObject("bankList", responseMsg.getData());
        return modelAndView;
    }

    /**
     * 转账
     *
     * @return
     */
    @RequestMapping("/transfer")
    public ModelAndView transferView(HttpSession session, Map<String, Object> model) {
        Users userPage = (Users) session.getAttribute(ConstantUtil.loginSession);
        ModelAndView modelAndView = new ModelAndView("userCenter");
        ResponseMsg responseMsg = pageDataService.systemTimeByClient(new Users());
        modelAndView.addObject("currentTime", responseMsg.getData());
        SystemMessage SystemMessage = new SystemMessage();
        SystemMessage.setUserId(userPage.getUserId());
        responseMsg =  pageDataService.systemUnreadMessageByClient(SystemMessage);
        modelAndView.addObject("unReadMsgNum", responseMsg.getData());
        responseMsg = pageDataService.getTransferPromotions(userPage.getUserId());
        modelAndView.addObject("promotionData", responseMsg.getData());
        modelAndView.addObject("pagename", "transfer");
        return modelAndView;
    }

    /**
     * 交易记录
     *
     * @return
     */
    @RequestMapping("/records")
    public ModelAndView recordsView(HttpSession session, Map<String, Object> model) {
        Users userPage = (Users) session.getAttribute(ConstantUtil.loginSession);
        ModelAndView modelAndView = new ModelAndView("userCenter");
        ResponseMsg responseMsg = pageDataService.systemTimeByClient(new Users());
        modelAndView.addObject("currentTime", responseMsg.getData());
        SystemMessage SystemMessage = new SystemMessage();
        SystemMessage.setUserId(userPage.getUserId());
        responseMsg =  pageDataService.systemUnreadMessageByClient(SystemMessage);
        modelAndView.addObject("unReadMsgNum", responseMsg.getData());
        modelAndView.addObject("pagename", "records");

        Object recordTypeSession = session.getAttribute(ConstantUtil.RECORD_TYPE);
        Object recordTimeRangeSession = session.getAttribute(ConstantUtil.RECORD_TIME_RANGE);
        if (recordTypeSession == null) {
            session.setAttribute(ConstantUtil.RECORD_TIME_RANGE, 1);
        }
        if (recordTimeRangeSession == null) {
            session.setAttribute(ConstantUtil.RECORD_TYPE, 1);
        }

        OperateRecords operateRecordsPage = new OperateRecords();
        operateRecordsPage.setUserId(userPage.getUserId());
        operateRecordsPage.setOperateType(recordTypeSession == null ? 1 : (Integer) recordTypeSession);
        operateRecordsPage.setTimeRange(recordTimeRangeSession == null ? 1 : (Integer) recordTimeRangeSession);
        ResponseMsg responseMsgPage = pageDataService.userRecordsByClient(operateRecordsPage);
        modelAndView.addObject("list", responseMsgPage.getData());
        return modelAndView;
    }

    @RequestMapping("/records/timeRange/{timeRange}")
    public ModelAndView recordsViewTimeRange(HttpSession session, @PathVariable("timeRange") Integer timeRange) {
        Users userPage = (Users) session.getAttribute(ConstantUtil.loginSession);
        ModelAndView modelAndView = new ModelAndView("userCenter");
        ResponseMsg responseMsg = pageDataService.systemTimeByClient(new Users());
        modelAndView.addObject("currentTime", responseMsg.getData());
        SystemMessage SystemMessage = new SystemMessage();
        SystemMessage.setUserId(userPage.getUserId());
        responseMsg =  pageDataService.systemUnreadMessageByClient(SystemMessage);
        modelAndView.addObject("unReadMsgNum", responseMsg.getData());
        modelAndView.addObject("pagename", "records");

        Object recordTypeSession = session.getAttribute(ConstantUtil.RECORD_TYPE);
        session.setAttribute(ConstantUtil.RECORD_TIME_RANGE, timeRange);

        OperateRecords operateRecordsPage = new OperateRecords();
        operateRecordsPage.setUserId(userPage.getUserId());
        operateRecordsPage.setOperateType(recordTypeSession == null ? 1 : (Integer) recordTypeSession);
        operateRecordsPage.setTimeRange(timeRange);
        ResponseMsg responseMsgPage = pageDataService.userRecordsByClient(operateRecordsPage);
        modelAndView.addObject("list", responseMsgPage.getData());
        return modelAndView;
    }

    @RequestMapping("/records/type/{recordType}")
    public ModelAndView recordsViewRecordType(HttpSession session, @PathVariable("recordType") Integer recordType) {
        Users userPage = (Users) session.getAttribute(ConstantUtil.loginSession);
        ModelAndView modelAndView = new ModelAndView("userCenter");
        ResponseMsg responseMsg = pageDataService.systemTimeByClient(new Users());
        modelAndView.addObject("currentTime", responseMsg.getData());
        SystemMessage SystemMessage = new SystemMessage();
        SystemMessage.setUserId(userPage.getUserId());
        responseMsg =  pageDataService.systemUnreadMessageByClient(SystemMessage);
        modelAndView.addObject("unReadMsgNum", responseMsg.getData());
        modelAndView.addObject("pagename", "records");

        Object recordTimeRangeSession = session.getAttribute(ConstantUtil.RECORD_TIME_RANGE);
        session.setAttribute(ConstantUtil.RECORD_TYPE, recordType);

        OperateRecords operateRecordsPage = new OperateRecords();
        operateRecordsPage.setUserId(userPage.getUserId());
        operateRecordsPage.setOperateType(recordType);
        operateRecordsPage.setTimeRange(recordTimeRangeSession == null ? 1 : (Integer) recordTimeRangeSession);
        ResponseMsg responseMsgPage = pageDataService.userRecordsByClient(operateRecordsPage);
        modelAndView.addObject("list", responseMsgPage.getData());
        return modelAndView;
    }

    /**
     * 银行卡
     *
     * @return
     */
    @RequestMapping("/bankList")
    public ModelAndView banklistView(HttpSession session, Map<String, Object> model) {
        Users userPage = (Users) session.getAttribute(ConstantUtil.loginSession);
        ModelAndView modelAndView = new ModelAndView("userCenter");
        ResponseMsg responseMsg = pageDataService.systemTimeByClient(new Users());
        modelAndView.addObject("currentTime", responseMsg.getData());
        SystemMessage SystemMessage = new SystemMessage();
        SystemMessage.setUserId(userPage.getUserId());
        responseMsg =  pageDataService.systemUnreadMessageByClient(SystemMessage);
        modelAndView.addObject("unReadMsgNum", responseMsg.getData());
        modelAndView.addObject("pagename", "bankList");

        UserBank userBank = new UserBank();
        userBank.setUserId(userPage.getUserId());
        responseMsg = pageDataService.userBankList(userBank);
        modelAndView.addObject("bankList", responseMsg.getData());
        return modelAndView;
    }

    /**
     * 账户设置
     *
     * @return
     */
    @RequestMapping("/accountSetting")
    public ModelAndView accountSettingView(HttpSession session, Map<String, Object> model) {
        Users userPage = (Users) session.getAttribute(ConstantUtil.loginSession);
        ModelAndView modelAndView = new ModelAndView("userCenter");
        ResponseMsg responseMsg = pageDataService.systemTimeByClient(new Users());
        modelAndView.addObject("currentTime", responseMsg.getData());
        SystemMessage SystemMessage = new SystemMessage();
        SystemMessage.setUserId(userPage.getUserId());
        responseMsg =  pageDataService.systemUnreadMessageByClient(SystemMessage);
        modelAndView.addObject("unReadMsgNum", responseMsg.getData());
        modelAndView.addObject("pagename", "accountSetting");
        return modelAndView;
    }

    /**
     * 消息中心
     *
     * @return
     */
    @RequestMapping("/systemMessageReceive")
    public ModelAndView systemMessageReceiveView(HttpSession session, Map<String, Object> model) {
        Users userPage = (Users) session.getAttribute(ConstantUtil.loginSession);
        ModelAndView modelAndView = new ModelAndView("userCenter");
        ResponseMsg responseMsg = pageDataService.systemTimeByClient(new Users());
        modelAndView.addObject("currentTime", responseMsg.getData());
        SystemMessage SystemMessage = new SystemMessage();
        SystemMessage.setUserId(userPage.getUserId());
        responseMsg =  pageDataService.systemUnreadMessageByClient(SystemMessage);
        modelAndView.addObject("unReadMsgNum", responseMsg.getData());
        modelAndView.addObject("pagename", "systemMessageReceive");

        SystemMessage systemMessage = new SystemMessage();
        systemMessage.setUserId(userPage.getUserId());
        responseMsg = pageDataService.systemMessageByClient(systemMessage);
        modelAndView.addObject("messageList", responseMsg.getData());
        return modelAndView;
    }

    @RequestMapping("/bettingRecord/timeRange/{timeRange}")
    public ModelAndView bettingRecordView(HttpSession session, @PathVariable("timeRange") Integer timeRange) {
        Users userPage = (Users) session.getAttribute(ConstantUtil.loginSession);
        ModelAndView modelAndView = new ModelAndView("userCenter");
        ResponseMsg responseMsg = pageDataService.systemTimeByClient(new Users());
        modelAndView.addObject("currentTime", responseMsg.getData());
        SystemMessage SystemMessage = new SystemMessage();
        SystemMessage.setUserId(userPage.getUserId());
        responseMsg =  pageDataService.systemUnreadMessageByClient(SystemMessage);
        modelAndView.addObject("unReadMsgNum", responseMsg.getData());
        modelAndView.addObject("pagename", "bettingRecord");

        Object recordTimeSession = session.getAttribute(ConstantUtil.RECORD_TIME_RANGE);
        session.setAttribute(ConstantUtil.RECORD_TIME_RANGE, timeRange);

        HgFlowData hgFlowData = new HgFlowData();
        hgFlowData.setAccountId(userPage.getUserName());
        hgFlowData.setTimeRange(recordTimeSession == null ? 1 : (Integer) recordTimeSession);
        responseMsg =  pageDataService.hgFlowData(hgFlowData);
        modelAndView.addObject("recordData", responseMsg.getData());
        return modelAndView;
    }

    /**
     * 优惠活动页面
     *
     * @param session
     * @param model
     * @return
     */
    @RequestMapping("/promotion")
    public ModelAndView promotionView(HttpSession session, Map<String, Object> model) {
        ModelAndView modelAndView = new ModelAndView("promotion");
        ResponseMsg responseMsg = pageDataService.systemTimeByClient(new Users());
        modelAndView.addObject("currentTime", responseMsg.getData());
        modelAndView.addObject("pageName", "promotion");

        Activity activityPage = new Activity();
        responseMsg =  pageDataService.promotionListByClient(activityPage);
        modelAndView.addObject("promotions", responseMsg.getData());

        return modelAndView;
    }

    /**
     * 真人娱乐页面
     *
     * @param session
     * @param model
     * @return
     */
    @RequestMapping("/zryl")
    public ModelAndView zrylView(HttpSession session, Map<String, Object> model) {
        ModelAndView modelAndView = new ModelAndView("meunZhenRenYuLe");
        ResponseMsg responseMsg = pageDataService.systemTimeByClient(new Users());
        modelAndView.addObject("currentTime", responseMsg.getData());
        modelAndView.addObject("pageName", "zryl");
        return modelAndView;
    }

    /**
     * 彩票页面
     *
     * @param session
     * @param model
     * @return
     */
    @RequestMapping("/lottery")
    public ModelAndView lotteryView(HttpSession session, Map<String, Object> model) {
        ModelAndView modelAndView = new ModelAndView("menuLottery");
        ResponseMsg responseMsg = pageDataService.systemTimeByClient(new Users());
        modelAndView.addObject("currentTime", responseMsg.getData());
        modelAndView.addObject("pageName", "lottery");
        return modelAndView;
    }

    /**
     * 移动客户端页面
     *
     * @param session
     * @param model
     * @return
     */
    @RequestMapping("/mobileClient")
    public ModelAndView mobileClientView(HttpSession session, Map<String, Object> model) {
        ModelAndView modelAndView = new ModelAndView("meunMobileClient");
        ResponseMsg responseMsg = pageDataService.systemTimeByClient(new Users());
        modelAndView.addObject("currentTime", responseMsg.getData());
        modelAndView.addObject("pageName", "mobileClient");
        return modelAndView;
    }

    /**
     * 电子游戏页面
     *
     * @param session
     * @param model
     * @return
     */
    @RequestMapping("/elecGame")
    public ModelAndView elecGameView(HttpSession session, Map<String, Object> model) {
        ModelAndView modelAndView = new ModelAndView("menuElecGame");
        ResponseMsg responseMsg = pageDataService.systemTimeByClient(new Users());
        modelAndView.addObject("currentTime", responseMsg.getData());
        modelAndView.addObject("pageName", "elecGame");
        return modelAndView;
    }

    /**
     * 帮助中心
     *
     * @param session
     * @param model
     * @return
     */
    @RequestMapping("/helpCenter")
    public ModelAndView helpCenterView(HttpSession session, Map<String, Object> model) {
        ModelAndView modelAndView = new ModelAndView("helpcenter");
        ResponseMsg responseMsg = pageDataService.systemTimeByClient(new Users());
        modelAndView.addObject("currentTime", responseMsg.getData());
        return modelAndView;
    }

    /**
     * 代理加盟
     *
     * @param session
     * @param model
     * @return
     */
    @RequestMapping("/agentJoin")
    public ModelAndView agentJoinView(HttpSession session, Map<String, Object> model) {
        ModelAndView modelAndView = new ModelAndView("agentJoin");
        ResponseMsg responseMsg = pageDataService.systemTimeByClient(new Users());
        modelAndView.addObject("currentTime", responseMsg.getData());
        return modelAndView;
    }

    /**
     * 返水规则
     *
     * @param session
     * @param model
     * @return
     */
    @RequestMapping("/returnRules")
    public ModelAndView returnRulesView(HttpSession session, Map<String, Object> model) {
        ModelAndView modelAndView = new ModelAndView("returnRules");
        ResponseMsg responseMsg = pageDataService.systemTimeByClient(new Users());
        modelAndView.addObject("currentTime", responseMsg.getData());
        return modelAndView;
    }

    /**
     * 等级优惠
     *
     * @param session
     * @param model
     * @return
     */
    @RequestMapping("/levelDiscount")
    public ModelAndView levelDiscountView(HttpSession session, Map<String, Object> model) {
        ModelAndView modelAndView = new ModelAndView("levelDiscount");
        ResponseMsg responseMsg = pageDataService.systemTimeByClient(new Users());
        modelAndView.addObject("currentTime", responseMsg.getData());
        return modelAndView;
    }

    /**
     * IP限制
     *
     * @param session
     * @param model
     * @return
     */
    @RequestMapping("/ip")
    public ModelAndView ipView(HttpSession session, Map<String, Object> model) {
        ModelAndView modelAndView = new ModelAndView("error/ip");
        return modelAndView;
    }

    @RequestMapping("/404")
    public ModelAndView get404ErrorPage(HttpSession session) {
        ModelAndView modelAndView = new ModelAndView("error/404");
        ResponseMsg responseMsg = pageDataService.systemTimeByClient(new Users());
        modelAndView.addObject("currentTime", responseMsg.getData());
        return modelAndView;
    }

    @RequestMapping("/403")
    public ModelAndView get403ErrorPage(HttpSession session) {
        ModelAndView modelAndView = new ModelAndView("error/403");
        ResponseMsg responseMsg = pageDataService.systemTimeByClient(new Users());
        modelAndView.addObject("currentTime", responseMsg.getData());
        return modelAndView;
    }

    @RequestMapping("/500")
    public ModelAndView get500ErrorPage(HttpSession session) {
        ModelAndView modelAndView = new ModelAndView("error/500");
        ResponseMsg responseMsg = pageDataService.systemTimeByClient(new Users());
        modelAndView.addObject("currentTime", responseMsg.getData());
        return modelAndView;
    }

    public PageDataService getPageDataService() {
        return pageDataService;
    }

    public void setPageDataService(PageDataService pageDataService) {
        this.pageDataService = pageDataService;
    }
}
